package vy.org.app.page.subscription.magazine;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.vy.dal.dao.ViewVyMemberSubscriptionDAO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDAO;
import org.vy.dal.dao.lookup.ViewVyLookupCitiesDO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDAO;
import org.vy.dal.dao.lookup.VyLookupPatrikaDO;
import org.vy.dal.dao.lookup.VyLookupPatrikaPeriodCostDAO;
import org.vy.dal.dao.lookup.VyLookupPatrikaPeriodCostDO;

import vy.org.app.common.AppConstants;
import vy.org.app.common.BaseAction;
import vy.org.app.common.ModeEnum;
import vy.org.app.common.ModuleEnum;

public class MntnMagazineSubscriptionAction extends BaseAction {

	@Override
	public ActionForward executeAction(ActionMapping mapping,
			ActionForm actionForm, HttpServletRequest request,
			HttpServletResponse response) {
		
		MntnMagazineSubscriptionForm form = (MntnMagazineSubscriptionForm)actionForm;
		try{
			List<VyLookupPatrikaDO> patrikaLst = VyLookupPatrikaDAO.findAllActive();
			List<VyLookupPatrikaPeriodCostDO> patrikaPeriodLst = VyLookupPatrikaPeriodCostDAO.findUniquePeriod();
			List<ViewVyLookupCitiesDO> countryLst = ViewVyLookupCitiesDAO.findUniqueCountries();
			List<ViewVyLookupCitiesDO> stateLst=null,cityLst=null;
			form.getAddress().setCountries(countryLst);
			if(form.getAddress().getCountry() != null && !"".equals(form.getAddress().getCountry())){
				stateLst = ViewVyLookupCitiesDAO.findStateByCountry(form.getAddress().getCountry());
			}
			form.getAddress().setStates(stateLst);
			if(form.getAddress().getStateOrRegion() != null && !"".equals(form.getAddress().getStateOrRegion())){
				cityLst = ViewVyLookupCitiesDAO.findCitiesByStateAndCountry(form.getAddress().getStateOrRegion(),form.getAddress().getCountry());
			}
			form.getAddress().setCities(cityLst);
			form.setPatrikaPeriodLst(patrikaPeriodLst);
			form.setPatrikas(patrikaLst);
		}catch(Exception exp){
			exp.printStackTrace();
		}
		
		if(form != null && form.getMode() != null){
			if(ModeEnum.SEARCH.getName().equals(form.getMode())){
				String where = " 1=1 ";
				if(form.getVyId() != null && !"".equals(form.getVyId().trim()))
					where += " AND VY_ID = '"+ form.getVyId() +"' ";
				if(form.getSubscriptionId() != null && !"".equals(form.getSubscriptionId().trim()))
					where += " AND SUBSCRIPTION_ID = '"+ form.getSubscriptionId() +"' ";
				if(form.getPatrikaPeriod() != -1)
					where += " AND PATRIKA_MONTH = "+ form.getPatrikaPeriod() +" ";
				if(form.getStartDateFrom() != null && !"".equals(form.getStartDateFrom().trim())){
					String ops = form.getStartDateOps();
					if(ops.equals(" BETWEEN "))
						where += " AND START_DATE "+ ops +" '"+ form.getStartDateFrom() +"' AND '"+ form.getStartDateTo() +"' ";
					else
						where += " AND START_DATE "+ ops +" '"+ form.getStartDateFrom() +"' ";
				}
				if(form.getEndDateFrom() != null && !"".equals(form.getEndDateFrom().trim())){
					String ops = form.getEndDateOps();
					if(ops.equals(" BETWEEN "))
						where += " AND END_DATE "+ ops +" '"+ form.getEndDateFrom() +"' AND '"+ form.getEndDateTo() +"' ";
					else
						where += " AND END_DATE "+ ops +" '"+ form.getEndDateFrom() +"' ";
				}
				if(form.getReceiptNo() != null && !"".equals(form.getReceiptNo().trim()))
					where += " AND RECEIPT_NO = '"+ form.getReceiptNo() +"' ";
				if(form.getReceiptDateFrom() != null && !"".equals(form.getReceiptDateFrom().trim())){
					String ops = form.getReceiptDateOps();
					if(ops.equals(" BETWEEN "))
						where += " AND RECEIPT_DATE "+ ops +" '"+ form.getReceiptDateFrom() +"' AND '"+ form.getReceiptDateTo() +"' ";
					else
						where += " AND RECEIPT_DATE "+ ops +" '"+ form.getReceiptDateFrom() +"' ";
				}
//				if(form.getStateOrRegion() != null && !"".equals(form.getStateOrRegion().trim()))
//					where += " VY_ID = "+ form.getVyId() +" ";
//				if(form.getCity() != null && !"".equals(form.getCity().trim()))
//					where += " VY_ID = "+ form.getVyId() +" ";
//				if(form.getDistrictOrCounty() != null && !"".equals(form.getDistrictOrCounty().trim()))
//					where += " VY_ID = "+ form.getVyId() +" ";
//				if(form.getCountry() != null && !"".equals(form.getCountry().trim()))
//					where += " VY_ID = "+ form.getVyId() +" ";
				try{
					request.setAttribute(AppConstants.RESULT, ViewVyMemberSubscriptionDAO.findByParam(where));
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return mapping.findForward("success");
	}

	@Override
	protected ModuleEnum getModuleId() {
		return ModuleEnum.SEARCH_SUBSCRIPTION;
	}

}
